import { FlowNode, FlowConnection } from '../common/types';

// 节点默认尺寸（需与CSS保持一致）
export const NODE_DEFAULT_WIDTH = 120;
export const NODE_DEFAULT_HEIGHT = 80;

/**
 * 计算两点之间的最优连接曲率 (0-1)
 */
export const calculateOptimalCurvature = (
  sourcePos: { x: number; y: number },
  targetPos: { x: number; y: number }
): number => {
  const dx = targetPos.x - sourcePos.x;
  const dy = targetPos.y - sourcePos.y;
  const distance = Math.sqrt(dx * dx + dy * dy);
  
  // 基于角度和距离的动态曲率
  const angle = Math.atan2(dy, dx);
  const normalizedAngle = (angle + Math.PI) / (2 * Math.PI);
  return 0.3 + (0.4 * Math.abs(normalizedAngle - 0.5));
};

/**
 * 获取动态连接点位置（智能锚点）
 */
export const getAdaptiveConnectionPoints = (
  sourceNode: FlowNode,
  targetNode: FlowNode
): {
  start: { x: number; y: number };
  end: { x: number; y: number };
  startDir: 'top' | 'right' | 'bottom' | 'left';
  endDir: 'top' | 'right' | 'bottom' | 'left';
} => {
  const sourceCenter = {
    x: sourceNode.position.x + NODE_DEFAULT_WIDTH / 2,
    y: sourceNode.position.y + NODE_DEFAULT_HEIGHT / 2
  };
  
  const targetCenter = {
    x: targetNode.position.x + NODE_DEFAULT_WIDTH / 2,
    y: targetNode.position.y + NODE_DEFAULT_HEIGHT / 2
  };

  const dx = targetCenter.x - sourceCenter.x;
  const dy = targetCenter.y - sourceCenter.y;
  
  // 确定连接点出口方向
  const getSourceAnchor = () => {
    if (Math.abs(dx) > Math.abs(dy)) {
      return dx > 0 
        ? { x: sourceNode.position.x + NODE_DEFAULT_WIDTH, y: sourceCenter.y, dir: 'right' }
        : { x: sourceNode.position.x, y: sourceCenter.y, dir: 'left' };
    } else {
      return dy > 0 
        ? { x: sourceCenter.x, y: sourceNode.position.y + NODE_DEFAULT_HEIGHT, dir: 'bottom' }
        : { x: sourceCenter.x, y: sourceNode.position.y, dir: 'top' };
    }
  };

  // 目标点方向与源点相反
  const getTargetAnchor = (sourceDir: string) => {
    switch(sourceDir) {
      case 'right': return { x: targetNode.position.x, y: targetCenter.y, dir: 'left' };
      case 'left': return { x: targetNode.position.x + NODE_DEFAULT_WIDTH, y: targetCenter.y, dir: 'right' };
      case 'bottom': return { x: targetCenter.x, y: targetNode.position.y, dir: 'top' };
      case 'top': return { x: targetCenter.x, y: targetNode.position.y + NODE_DEFAULT_HEIGHT, dir: 'bottom' };
      default: return { x: targetCenter.x, y: targetCenter.y, dir: 'right' };
    }
  };

  const sourceAnchor = getSourceAnchor();
  const targetAnchor = getTargetAnchor(sourceAnchor.dir);

  return {
    start: { x: sourceAnchor.x, y: sourceAnchor.y },
    end: { x: targetAnchor.x, y: targetAnchor.y },
    startDir: sourceAnchor.dir as any,
    endDir: targetAnchor.dir as any
  };
};

/**
 * 生成贝塞尔曲线路径
 */
export const generateBezierPath = (
  start: { x: number; y: number },
  end: { x: number; y: number },
  curvature: number = 0.5
): string => {
  const cp1 = {
    x: start.x + (end.x - start.x) * curvature,
    y: start.y
  };
  const cp2 = {
    x: end.x - (end.x - start.x) * curvature,
    y: end.y
  };

  return `M ${start.x} ${start.y} C ${cp1.x} ${cp1.y}, ${cp2.x} ${cp2.y}, ${end.x} ${end.y}`;
};

/**
 * 检查连接线碰撞
 */
export const checkConnectionCollision = (
  conn1: FlowConnection,
  conn2: FlowConnection,
  nodes: FlowNode[]
): boolean => {
  // 实现基于向量数学的线段相交检测
  // 简化版实现（实际项目应使用空间索引优化）
  return false;
};